package org.dizena.modules.wss.web.controller;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.dizena.base.avo.resp.Result;
import org.dizena.base.enums.MessageState;
import org.dizena.base.mq.MqQueue;
import org.dizena.modules.wss.bean.Message;
import org.dizena.modules.wss.service.WsService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping("chat/msg")
public class ChatController
{

    @Resource
    private WsService service;
    @Resource
    private MqQueue mq;

    /**
     * 发送消息
     */
    @PostMapping("send")
    public Result<String> send(Message msg)
    {
        if (StringUtils.isEmpty(msg.getType()))
        {
            msg.setType("text");
        }
        msg = service.saveMessage(msg);

        Boolean flag = service.sendMessageToUser(msg);
        if (!flag)
        {
            mq.product(msg);
        }
        return Result.success(msg.getId());
    }

    /**
     * 更新已读消息状态
     */
    @GetMapping("read/{id}")
    public Result<Boolean> read(@PathVariable("id") String id)
    {
        service.updateMessageStatus(id, MessageState.ReceiverHasRead);
        return Result.success(true);
    }

    /**
     * WSS服务连接
     */
    @GetMapping("link")
    public Result<String> link()
    {
        return Result.success(service.getLinkHost());
    }


}